package JFrame.Admin.MailManager;

import entity.Mail;
import serive.AdminService;
import serive.AdminServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @className: AdminQueryMailJFrame
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2021/12/29
 **/
public class AdminQueryMailJFrame extends JFrame {
    JLabel l_id;
    JTextField t_id;
    JButton b_query;
    JTable table_book;
    AdminService adminService;
    JFrame jFrame=this;

    public AdminQueryMailJFrame() {
        //页面大小不可变
        setResizable(false);
        this.setTitle("查询快件窗体");
        this.setSize(700, 800);
        init();
        this.setVisible(true);

    }

    public void init() {
        this.setLayout(null);
        adminService =new AdminServiceImpl();
        List<Mail> allMail = adminService.queryAllMail();
        JLabel l_tip = new JLabel("以下是所有快件:");
        l_id = new JLabel("快件编号");
        t_id = new JTextField();
        b_query = new JButton("查询");
        Object[] title = {"快件ID", "收件人ID", "名称", "到站时间", "派送状况", "派送完成时间"};
        Object[][] mailLists = new String[allMail.size()+1][7];
        for(int i = 0; i < allMail.size() ; i++) {
            for(int j = 0; j < 6; j++) {
                switch(j) {
                    case 0: mailLists[i][j] =allMail.get(i).getmId();break;
                    case 1: mailLists[i][j] =allMail.get(i).getuId();break;
                    case 2: mailLists[i][j] =allMail.get(i).getmName();break;
                    case 3: mailLists[i][j] =allMail.get(i).getArrivalTime();break;
                    case 4: mailLists[i][j] =allMail.get(i).getMailState();break;
                    case 5: mailLists[i][j] =allMail.get(i).getCompletionTime();break;
                }
            }
        }


        l_tip.setBounds(10, 50, 200, 30);
        b_query.setBounds(190, 10, 80, 30);
        b_query.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new queryByMid(t_id.getText());
            }
        });

        table_book = new JTable(mailLists, title);
        table_book.setSize(650, 500);

        l_id.setBounds(10, 10, 80, 30);
        t_id.setBounds(100, 10, 80, 30);
        this.add(l_id);
        this.add(t_id);
        this.add(l_tip);
        this.add(b_query);

        JScrollPane pane = new JScrollPane(table_book);
        pane.setBounds(10, 80, 670, 500);
        this.add(pane);

    }


}
class queryByMid extends JFrame {
    JTable table_book;
    AdminService adminService;
    String mId;
    JFrame jFrame=this;

    public queryByMid(String mId) {
        //页面大小不可变
        setResizable(false);
        this.mId=mId;
        this.setSize(550, 500);
        this.setTitle("以下是编号为"+mId+"的快件:");
        init();


    }

    public void init() {
        adminService= new AdminServiceImpl();
        List<Mail> mailList = adminService.queryMail(mId);
        if (mailList.isEmpty()){
            JOptionPane.showMessageDialog(this,
                    "请输入正确的快件ID", "！！！",JOptionPane.WARNING_MESSAGE);
            this.setVisible(false);
        }else {
            this.setVisible(true);
        }
        this.setLayout(null);

        Object[] title = {"快件ID", "收件人ID", "名称", "到站时间", "派送状况", "派送完成时间"};

        Object[][] mailLists = new String[mailList.size() + 1][7];
        for (int i = 0; i < mailList.size(); i++) {
            for (int j = 0; j < 6; j++) {
                switch (j) {
                    case 0:
                        mailLists[i][j] = mailList.get(i).getmId();
                        break;
                    case 1:
                        mailLists[i][j] = mailList.get(i).getuId();
                        break;
                    case 2:
                        mailLists[i][j] = mailList.get(i).getmName();
                        break;
                    case 3:
                        mailLists[i][j] = mailList.get(i).getArrivalTime();
                        break;
                    case 4:
                        mailLists[i][j] = mailList.get(i).getMailState();
                        break;
                    case 5:
                        mailLists[i][j] = mailList.get(i).getCompletionTime();
                        break;
                }
            }
        }
        table_book = new JTable(mailLists, title);
        table_book.setSize(500, 300);
        JScrollPane pane = new JScrollPane(table_book);
        pane.setBounds(10, 80, 500, 300);
        this.add(pane);
    }
}